package io.bluefw.microapp.core;

import java.io.IOException;

import org.apache.commons.lang3.reflect.FieldUtils;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

@SuppressWarnings("rawtypes")
public class EnumSerializer extends JsonSerializer<Enum> {

	@Override
	public void serialize(Enum value, JsonGenerator gen, SerializerProvider serializers)
			throws IOException, JsonProcessingException {
		gen.writeStartObject();  
		Globalize globalize = FieldUtils.getField(value.getClass(), value.name()).getAnnotation(Globalize.class);
		gen.writeStringField("name", globalize.value());
		gen.writeNumberField("index", value.ordinal());
		gen.writeStringField("value", value.name());
		gen.writeEndObject();
	}

}
